﻿using System.Data;
using System.Data.Common;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.OutputDefinitions
{
    using Songhay.Data.GenericWeb.Procedures;
    using Songhay.Xml;

    /// <summary>
    /// Output procedures from the DBMS
    /// for <see cref="System.Xml.XPath.IXPathNavigable"/> types.
    /// </summary>
    public static partial class XPathNavigableOutput
    {
        /// <summary>
        /// Gets the search results.
        /// </summary>
        /// <param name="commonConnection">The common connection.</param>
        /// <param name="navigableInput">The navigable input.</param>
        /// <returns>Returns <see cref="System.Xml.XPath.XPathDocument"/>.</returns>
        public static XPathDocument GetSearchResults(DbConnection commonConnection, IXPathNavigable navigableInput)
        {
            XPathNavigator n = XmlUtility.GetNavigableNode(navigableInput, "//SearchText");
            if(n == null) throw new DataException("SearchText node not found.");

            string s = List.Search(commonConnection, n.Value);

            if(string.IsNullOrEmpty(s))
            {
                return XmlUtility.GetInternalMessageDocument("Your Search Returned No Results", new string[] { string.Format("<strong>Search:</strong> <em>{0}</em>", n.Value) });
            }
            else return XmlUtility.GetNavigableDocument(s);
        }
    }
}
